<?php

namespace app\index\model;
use app\api\controller\PublicApi;
use think\facade\Db;
use app\common\model\Common;

class CardsTransOnly extends Common
{
    /**
     * 返回layui的table所需要的格式
     * @param $post
     * @param $user_id
     * @return mixed
     * @throws \think\db\exception\DbException
     */
    public function tableData($post)
    {
        $list_rows  = $post['limit'] ?? config('paginate.list_rows');
        $page       = $post['page'] ?? config('paginate.page');

        $where =[];
        $where[] = ['u.id', '=', $post['user_id']];
        if(isset($post['card_num']) && !empty($post['card_num'])){
            $create_card_param      = [
                "user_id"       => 1,
                "action_type"   => "search_trans_only",
                "post_param"  => [
                    "card_num"   => $post['card_num'],
                    "startDate"    => date("Y-m-d",strtotime("-44 day"))
                ]
            ];
            $request_api= PublicApi::requestApi("Comdata",$create_card_param);
            $blog       = $request_api['blog'] ?? false;
            if($blog){
                $list = $request_api['list'];
                Db::query("truncate mar_cards_trans_only");
                foreach ($list as $k=>$v){
                    $cardNumber = $v['cardNumber'];
                    $postedDate = $v['postedDate'] ?? '';
                    $utime      = !empty($postedDate) ? $v['postedDate'] . ' ' . $v['postedTime'] : '';
                    $shopping   = $v['merchantName'] ?? '';
                    if(empty($shopping)){
                        $tran_acceptLocation = $v['acceptLocation'] ?? '';
                        if(!empty($tran_acceptLocation)){
                            $location_array = explode(" " , $tran_acceptLocation);
                            $shopping       = ($location_array[0] ?? '') . ' '. ($location_array[1] ?? ''); //组合数组前两个字符串
                        }
                    }
                    Db::name('cards_trans_only')->save([
                        'app_code'      => $v['approvalCode'],
                        'card_num'      => $cardNumber,
                        'tran_status'   => $v['transactionStatus'],
                        'amount_auth'   => $v['preAuthAmount'],
                        'amount_post'   => $v['postedAmount'],
                        'shopping'      => $shopping,
                        'reversal_flag' => $v['reversalFlag'],
                        'ctime'         => $v['transactionDate'] . ' ' . $v['transactionTime'],
                        'utime'         => $utime,
                    ]);
                }
            }
        }
        $list = Db::name('cards_trans_only')->alias('ct')
            ->leftJoin('cards c','c.card_num=ct.card_num')
            ->leftJoin('user u' , 'u.id=c.user_id')
            ->field('u.username,c.first_name,c.last_name,c.card_num,ct.*')
            ->order('ct.ctime desc')
            ->where($where)
            ->paginate(['list_rows' => $list_rows, 'page' => $page])
            ->toArray();
        return $result = [
            'code'  => 0,
            'msg'   => lang('get info success'),
            'data'  => $list['data'],
            'count' => $list['total'],
        ];
    }
}